<template>
  <div>
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon name="cross" size="0.37333334rem" color="white" @click="closePop"/>
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>我的频道
          <span class="small-title">
            点击{{delShow?'删除':'进入'}}频道
          </span>
        </span>
        <span @click="changeDelShow">{{delShow?'完成':'编辑'}}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="item in channelArr" :key="item.id" @click="intoChannel(item.id)">
          <div class="channel-item van-hairline--surround">
            {{item.name}}
            <!-- 删除的徽标 -->
            <van-badge color="transparent" class="cross-badge" v-show="item.name==='推荐'?false:delShow">
              <template #content>
                <van-icon
                @click.stop="delChanell((item.id))"
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="obj in unChoose" :key="obj.id">
          <div class="channel-item van-hairline--surround" @click="addChannel(obj.id)">{{obj.name}}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { getUserChannels, setUpChannel, delChannels } from '@/api'
import { mapActions, mapState } from 'vuex'
export default {
  data () {
    return {
      delShow: false,
      channelArr: []
    }
  },
  computed: {
    ...mapState('home', ['allChannel']),
    unChoose () {
      return this.allChannel.filter(item1 => {
        const index = this.channelArr.findIndex(item2 => item2.id === item1.id)
        if (index === -1) return true
      })
    }
  },
  created () {
    this.getUserChannelsList()
    this.setAllChannelActions()
  },
  methods: {
    // 获取用户频道
    async getUserChannelsList () {
      const res = await getUserChannels()
      this.channelArr = res.data.data.channels
    },
    ...mapActions('home', ['setAllChannelActions']),
    closePop () {
      this.$emit('closePop', 0)
      this.delShow = false
    },
    // 删除频道
    async delChanell (id) {
      await delChannels(id)
      this.getUserChannelsList()
    },
    // 编辑按钮
    changeDelShow () {
      this.delShow = !this.delShow
    },
    // 添加标签
    async addChannel (id) {
      if (this.delShow) {
        const index = this.allChannel.findIndex(obj => obj.id === id)
        console.log(index)
        const arr = [{
          id: this.allChannel[index].id,
          seq: index + 1
        }]
        await setUpChannel(arr)
        this.getUserChannelsList()
      }
    },
    intoChannel (activeId) {
      this.$emit('closePop', activeId)
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}
.van-nav-bar {
  background: #007bff;
}
</style>
